package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
        /*
        * 添加检查组
        *1. 首先把检查组数据添加到t_checkgroup这张表中
        * 2.其次在中间表（t_checckgroup_checkitem）中建立检查组和检查项的对应关系
        * */

        @Reference
        private CheckGroupService checkGroupService;

        @RequestMapping("/add")
        public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
            try{
                checkGroupService.add(checkGroup,checkitemIds);
                return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);

            }catch (Exception e){
                e.printStackTrace();
                return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
            }

        }

        @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean  queryPageBean){

           PageResult result =  checkGroupService.findPage(queryPageBean);
            return result;
        }

        @RequestMapping("/findById")
    public  Result findById(int id){
            try{
                Map map = checkGroupService.findById(id);
                return  new Result(true,null,map);
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false,"服务器正忙，请稍后访问");
            }
        }

        @RequestMapping("/modify")
    public Result modify(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
            try{
                checkGroupService.modify(checkGroup,checkitemIds);
                return  new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
            }
        }
        @RequestMapping("/findAll")
        public Result findAll(){
            try{
                List<CheckGroup> checkGroupList = checkGroupService.findAll();
                return  new Result(true,null,checkGroupList);
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false,"服务器正忙，请稍后尝试");
            }
        }

}
